#include "uuidutil.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

uuidutil::uuidutil()
{
}


uuidutil::~uuidutil()
{
}

int uuidutil::ByteToWinUuidString(const unsigned char* data, int datalen, char* string, int stringlen)
{
	int iret = 0;
	sprintf(string, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", data[3], data[2], data[1], data[0], 
		data[5], data[4], data[7], data[6], data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15]);

	return iret;
}

int uuidutil::ByteFromWinUuidString(unsigned char* data, int datalen, const char* string, int stringlen)
{
	int iret = 0;

	char data1[16] = {0};
	if (datalen < (stringlen-4) % 2){
		iret = -1;
	}
	else{
		const char* p = string;
		int i = 0;
		int j = 0;
		while (i< stringlen){
			if (p[0] == '-'){
				p++;
				i++;
			}
			else{
				char buff[3] = {0};
				strncpy(buff, p, 2);
				char* pEnd = NULL;
				data1[j++] = strtol(buff, &pEnd, 16);
				p += 2;
				i += 2;
			}
		}
	}

	data[0] = data1[3];
	data[1] = data1[2];
	data[2] = data1[1];
	data[3] = data1[0];

	data[4] = data1[5];
	data[5] = data1[4];

	data[6] = data1[7];
	data[7] = data1[6];

	memcpy(&data[8], &data1[8], 8);

	return iret;
}

int uuidutil::ByteToLinuxUuidString(const unsigned char* data, int datalen, char* string, int stringlen)
{
	int iret = 0;

	if (stringlen < (datalen * 2 + 4)){
		iret = -1;
	}
	else
	sprintf(string, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15]);

	return iret;
}

int uuidutil::ByteFromLinuxUuidString(unsigned char* data, int datalen, const char* string, int stringlen)
{
	int iret = 0;

	if (datalen < (stringlen - 4) % 2){
		iret = -1;
	}
	else{
		const char* p = string;
		int i = 0;
		int j = 0;
		while (i< stringlen){
			if (p[0] == '-'){
				p++;
				i++;
			}
			else{
				char buff[3] = { 0 };
				strncpy(buff, p, 2);
				char* pEnd = NULL;
				data[j++] = strtol(buff, &pEnd, 16);
				p += 2;
				i += 2;
			}
		}
	}

	return iret;
}

int uuidutil::ByteToHexString(const unsigned char* data, int datalen, char* string, int stringlen)
{
	int iret = 0;

	if (stringlen < 2 * datalen){
		iret = -1;
	}
	else{
		int i = 0;
		while (i < datalen){
			char buff[3] = {0};
			sprintf(buff,"%x", data[i]);
			if (buff[1] == '\0'){
				strcat(string, "0");
			}
			strcat(string, buff);
			i++;
		}
	}

	return iret;
}

int uuidutil::ByteFromHexString(unsigned char* data, int datalen, const char* string, int stringlen)
{
	int iret = 0;
	if (stringlen % 2 > datalen){
		iret = -1;
	}
	else{
		const char* p = string;
		char buff[3] = { 0 };
		int i = 0;
		while (p && (p-string) < stringlen){
			memset(buff, 0, 3);
			strncpy(buff, p, 2);
			char* pEnd = NULL;
			data[i++] = strtol(buff,&pEnd, 16);
			p += 2;
		}
	}

	return iret;
}